<?php
class SHML_Utility
{

	public function utility_case($properties)
	{
		$v = strtolower($properties['attributes']['value']);
		$t = strtolower($properties['innerHTML']);

		switch ($v) {
			case "upper":
			$t = strtoupper($t);
			break;
			
			case "sentence":
			$t = ucwords($t);
			break;
			
			case "each":
			$t = ucwords($t);
			break;
			
			default:
			$t = $t;
			break;
		}
		
		return $t;
	}
	
	public function utility_check_browser($properties)
	{
		$attributes = $properties['attributes'];
		$attr = array();
		$found = array();
		$find = array("java","ismobile","iscrawler","isreader");
		$replace = array("javaapplets","ismobiledevice","crawler","issyndicationreader");
		
		$bc = new Browscap();
		$agent = $bc->getBrowser();

		//Browser
		if (isset($attributes['browser'])) {
			$found['browser'] = (strtolower($attributes['browser']) == strtolower($agent->browser)) ? 1 : -1;
		}

		//OS control
		if (isset($attributes['platform'])) {
			$found['platform'] = (stristr($agent->platform, $attributes['platform'])) ? 1 : -1;
		}
		
		//Version Controls (browser and CSS)
		if (isset($attributes['version']) || isset($attributes['cssversion']) || isset($attributes['css'])) {
			$tmp = array();
			$tmp['version'] = (isset($attributes['version'])) ? $attributes['version'] : NULL;
			$tmp['cssversion'] = (isset($attributes['cssversion'])) ? $attributes['cssversion'] : NULL;
			foreach ($tmp as $k => $v) {
				if (!is_null($v)) {
					$o = (substr($v,1,1) == "=") ? substr($v,0,2) : substr($v,0,1);
					switch ($o) {
						case "-=":
						$found[$k] = ($agent->$k <= substr($v,2)) ? 1 : -1;
						break;
				
						case "+=":
						$found[$k] = ($agent->$k >= substr($v,2)) ? 1 : -1;
						break;
						
						case "-":
						$found[$k] = ($agent->$k < substr($v,1)) ? 1 : -1;
						break;
				
						case "+":
						$found[$k] = ($agent->$k > substr($v,1)) ? 1 : -1;
						break;
						
						default:
						$found[$k] = ($agent->$k == $v) ? 1 : -1;
						break;
					}
				}
			}
		}
		
		//True/False Attributes
		foreach ($attributes as $k => $v) {
			if (!isset($found[$k])) {
				$v = (strtolower($v) == "true") ? 1 : 0;
				$k = str_replace($find, $replace, strtolower($k));
				$found[$k] = ($v == strtolower($agent->$k)) ? 1 : -1;
			}
		}
		
		//If one attribute does not match, kill it
		//Otherwise replace
		if (in_array(-1,$found)) {
			return "";
		}
		else {
			return $properties['innerHTML'];
		}
	}
	
	public function utility_datetime($properties)
	{	
		$v = $properties['attributes']['value'];
		return ($v != "") ? date($v) : date("m/d/Y - h:i:s A");
	}
	
	public function utility_if_exists($properties)
	{
		$v = $properties['attributes']['value'];
		$l = strtolower($properties['attributes']['logic']);
		$list = $properties['var_list'];
		
		if ($l == "false") {
			return (!isset($list[$v])) ? $properties['innerHTML'] : "";
		}
		else {
			return (isset($list[$v])) ? $properties['innerHTML'] : "";
		}
	}
	
	public function utility_include($properties)
	{
		$p = $properties['attributes']['template_dir'] . $properties['attributes']['path'];
		$f = @file_get_contents($p);
		return ($f !== FALSE) ? $f : "<!--$p could not be opened.-->";
	}
	
	public function utility_get_browser($properties)
	{
		$bc = new Browscap();
		$agent = $bc->getBrowser();
		$v = $properties['attributes']['value'];
		if (!empty($v)) {
			foreach ($agent as $k => $vv) {
				$v = str_ireplace("#" . $k, $vv, $v);
			}
			return $v;
		}
		else {
			return $agent->browser . "/" . $agent->version . " on " . $agent->platform;
		}
	}
	
	public function utility_loop($properties)
	{
		$attributes = $properties['attributes'];
		$alt = $attributes['alternators'];
		$c = ($attributes['columns'] < 1) ? 1 : $attributes['columns'];
		$l = $attributes['limit'];
		$v = $attributes['value'];
		
		$a = array();
		$innerHTML = $properties['innerHTML'];
		$new_HTML = array();
		$results = $properties['var_list'][$v];
		$tmp_HTML = NULL;
		$tracker = array();
		
		if (!empty($alt)) {
			$alt = explode("}", $alt);
			foreach ($alt as $k => $vv) {
				if (!empty($vv)) {
					$vv = str_replace(($k + 1) . ":{", "", $vv);
					$a[$k] = explode(",", $vv);
				}
			}
		}
	
		if (isset($properties['var_list'][$v])) {
		
			foreach ($results as $k => $vv) {
				$max = @count($results[$k]);
				break;
			}

			$l = ($l > $max || $l < 1) ? $max : $l;
		
			for ($i = 0; $i < $l; $i++) {
				$tmp_HTML = $innerHTML;
				
				for ($x = 0; $x < @count($a); $x++) {
					$tracker[$x] = (!array_key_exists($x, $tracker) || $tracker[$x] >= (@count($a[$x]) - 1)) ? 0 : $tracker[$x] + 1;
					$tmp_HTML = preg_replace("/(#" . ($x + 1) . "#)/i", str_replace("]]", ">", str_replace("[[", "<", $a[$x][$tracker[$x]])), $tmp_HTML);
				}
			
				foreach ($results as $k => $vv) {
					$tmp_HTML = preg_replace("/(#" . $k . "#)/i", $results[$k][$i], $tmp_HTML);
				}
				
				array_push($new_HTML, $tmp_HTML);
			}	
		}
		
		return implode("\n", $new_HTML);
	}
	
	public function utility_truncate($properties)
	{
		$l = $properties['attributes']['length'];
		$l = ($l != "" && is_numeric($l)) ? $l : 72;
		$t = $properties['innerHTML'];
		if (strtolower($properties['attributes']['break']) == "true") {
			$t = wordwrap($t, $l, "<br />\n",FALSE);
		}
		else {
			if (strlen($t) > $l) {
				do {
					$l -= 1;
					$chr = substr($t,$l,1);
				} while ($chr != " " && $l > 0);
				$l = ($l <= 0) ? $properties['attributes']['length'] : $l;
				$t = substr($t,0,$l);
			}
			if (strtolower($properties['attributes']['ellipsis']) == "true" && strlen($t) >= $l) { $t .= "..."; }
		}
		return $t;
	}
	
	public function utility_variable($properties)
	{
		$v = $properties['attributes']['value'];
		$c = $properties['attributes']['case'];
		$list = $properties['var_list'];
		$result = NULL;
		
		if (isset($list[$v])) {
			if (!empty($c)) {
				$arr = array();
				$arr['innerHTML'] = $list[$v];
				$arr['attributes']['value'] = $c;
				$result = $this->utility_case($arr);
			}
			return (is_null($result)) ? $list[$v] : $result;
		}
		else {
			return "<!--Variable ($v) is not defined. Please check the spelling or if it exists.-->";
		}
	}
	
}